<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/doc/src/examples/windowflags.qdoc -->
<head>
  <title>Qt 4.3: Window Flags Example</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">Window Flags Example<br /><small></small></h1>
<p>Files:</p>
<ul>
<li><a href="widgets-windowflags-controllerwindow-cpp.html">widgets/windowflags/controllerwindow.cpp</a></li>
<li><a href="widgets-windowflags-controllerwindow-h.html">widgets/windowflags/controllerwindow.h</a></li>
<li><a href="widgets-windowflags-previewwindow-cpp.html">widgets/windowflags/previewwindow.cpp</a></li>
<li><a href="widgets-windowflags-previewwindow-h.html">widgets/windowflags/previewwindow.h</a></li>
<li><a href="widgets-windowflags-main-cpp.html">widgets/windowflags/main.cpp</a></li>
</ul>
<p>The Window Flags example shows how to use the window flags available in Qt.</p>
<p>A window flag is either a type or a hint. A type is used to specify various window-system properties for the widget. A widget can only have one type, and the default is <a href="qt.html#WindowType-enum">Qt::Widget</a>. However, a widget can have zero or more hints. The hints are used to customize the appearance of top-level windows.</p>
<p>A widget's flags are stored in a <a href="qt.html#WindowType-enum">Qt::WindowFlags</a> type which stores an OR combination of the flags.</p>
<p align="center"><img src="images/windowflags-example.png" alt="Screenshot of the Window Flags example" /></p><p>The example consists of two classes:</p>
<ul>
<li><tt>ControllerWindow</tt> is the main application widget that allows the user to choose among the available window flags, and displays the effect on a separate preview window.</li>
<li><tt>PreviewWindow</tt> is a custom widget displaying the name of its currently set window flags in a read-only text editor.</li>
</ul>
<p>We will start by reviewing the <tt>ControllerWindow</tt> class, then we will take a look at the <tt>PreviewWindow</tt> class.</p>
<a name="controllerwindow-class-definition"></a>
<h2>ControllerWindow Class Definition</h2>
<pre> class ControllerWindow : public QWidget
 {
     Q_OBJECT

 public:
     ControllerWindow();

 private slots:
     void updatePreview();

 private:
     void createTypeGroupBox();
     void createHintsGroupBox();
     QCheckBox *createCheckBox(const QString &amp;text);
     QRadioButton *createRadioButton(const QString &amp;text);

     PreviewWindow *previewWindow;

     QGroupBox *typeGroupBox;
     QGroupBox *hintsGroupBox;
     QPushButton *quitButton;

     QRadioButton *windowRadioButton;
     QRadioButton *dialogRadioButton;
     QRadioButton *sheetRadioButton;
     QRadioButton *drawerRadioButton;
     QRadioButton *popupRadioButton;
     QRadioButton *toolRadioButton;
     QRadioButton *toolTipRadioButton;
     QRadioButton *splashScreenRadioButton;

     QCheckBox *msWindowsFixedSizeDialogCheckBox;
     QCheckBox *x11BypassWindowManagerCheckBox;
     QCheckBox *framelessWindowCheckBox;
     QCheckBox *windowTitleCheckBox;
     QCheckBox *windowSystemMenuCheckBox;
     QCheckBox *windowMinimizeButtonCheckBox;
     QCheckBox *windowMaximizeButtonCheckBox;
     QCheckBox *windowContextHelpButtonCheckBox;
     QCheckBox *windowShadeButtonCheckBox;
     QCheckBox *windowStaysOnTopCheckBox;
     QCheckBox *customizeWindowHintCheckBox;
 };</pre>
<p>The <tt>ControllerWindow</tt> class inherits <a href="qwidget.html">QWidget</a>. The widget allows the user to choose among the available window flags, and displays the effect on a separate preview window.</p>
<p>We declare a private <tt>updatePreview()</tt> slot to refresh the preview window whenever the user changes the window flags.</p>
<p>We also declare several private functions to simplify the constructor: We call the <tt>createTypeGroupBox()</tt> function to create a radio button for each available window type, using the private <tt>createButton()</tt> function, and gather them within a group box. In a similar way we use the <tt>createHintsGroupBox()</tt> function to create a check box for each available hint, using the private <tt>createCheckBox()</tt> function.</p>
<p>In addition to the various radio buttons and checkboxes, we need an associated <tt>PreviewWindow</tt> to show the effect of the currently chosen window flags.</p>
<p align="center"><img src="images/windowflags_controllerwindow.png" alt="Screenshot of the Controller Window" /></p><a name="controllerwindow-class-implementation"></a>
<h2>ControllerWindow Class Implementation</h2>
<pre> ControllerWindow::ControllerWindow()
 {
     previewWindow = new PreviewWindow(this);

     createTypeGroupBox();
     createHintsGroupBox();

     quitButton = new QPushButton(tr(&quot;&amp;Quit&quot;));
     connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));

     QHBoxLayout *bottomLayout = new QHBoxLayout;
     bottomLayout-&gt;addStretch();
     bottomLayout-&gt;addWidget(quitButton);

     QVBoxLayout *mainLayout = new QVBoxLayout;
     mainLayout-&gt;addWidget(typeGroupBox);
     mainLayout-&gt;addWidget(hintsGroupBox);
     mainLayout-&gt;addLayout(bottomLayout);
     setLayout(mainLayout);

     setWindowTitle(tr(&quot;Window Flags&quot;));
     updatePreview();
 }</pre>
<p>In the constructor we first create the preview window. Then we create the group boxes containing the available window flags using the private <tt>createTypeGroupBox()</tt> and <tt>createHintsGroupBox()</tt> functions. In addition we create a <b>Quit</b> button. We put the button and a stretchable space in a separate layout to make the button appear in the <tt>WindowFlag</tt> widget's right bottom corner.</p>
<p>Finally, we add the button's layout and the two goup boxes to a <a href="qvboxlayout.html">QVBoxLayout</a>, set the window title and refresh the preview window using the <tt>updatePreview()</tt> slot.</p>
<pre> void ControllerWindow::updatePreview()
 {
     Qt::WindowFlags flags = 0;

     if (windowRadioButton-&gt;isChecked()) {
         flags = Qt::Window;
     } else if (dialogRadioButton-&gt;isChecked()) {
         flags = Qt::Dialog;
     } else if (sheetRadioButton-&gt;isChecked()) {
         flags = Qt::Sheet;
     } else if (drawerRadioButton-&gt;isChecked()) {
         flags = Qt::Drawer;
     } else if (popupRadioButton-&gt;isChecked()) {
         flags = Qt::Popup;
     } else if (toolRadioButton-&gt;isChecked()) {
         flags = Qt::Tool;
     } else if (toolTipRadioButton-&gt;isChecked()) {
         flags = Qt::ToolTip;
     } else if (splashScreenRadioButton-&gt;isChecked()) {
         flags = Qt::SplashScreen;
     }</pre>
<p>The <tt>updatePreview()</tt> slot is called whenever the user changes any of the window flags. First we create an empty <a href="qt.html#WindowType-enum">Qt::WindowFlags</a> <tt>flags</tt>, then we determine which one of the types that is checked and add it to <tt>flags</tt>.</p>
<pre>     if (msWindowsFixedSizeDialogCheckBox-&gt;isChecked())
         flags |= Qt::MSWindowsFixedSizeDialogHint;
     if (x11BypassWindowManagerCheckBox-&gt;isChecked())
         flags |= Qt::X11BypassWindowManagerHint;
     if (framelessWindowCheckBox-&gt;isChecked())
         flags |= Qt::FramelessWindowHint;
     if (windowTitleCheckBox-&gt;isChecked())
         flags |= Qt::WindowTitleHint;
     if (windowSystemMenuCheckBox-&gt;isChecked())
         flags |= Qt::WindowSystemMenuHint;
     if (windowMinimizeButtonCheckBox-&gt;isChecked())
         flags |= Qt::WindowMinimizeButtonHint;
     if (windowMaximizeButtonCheckBox-&gt;isChecked())
         flags |= Qt::WindowMaximizeButtonHint;
     if (windowContextHelpButtonCheckBox-&gt;isChecked())
         flags |= Qt::WindowContextHelpButtonHint;
     if (windowShadeButtonCheckBox-&gt;isChecked())
         flags |= Qt::WindowShadeButtonHint;
     if (windowStaysOnTopCheckBox-&gt;isChecked())
         flags |= Qt::WindowStaysOnTopHint;
     if (customizeWindowHintCheckBox-&gt;isChecked())
         flags |= Qt::CustomizeWindowHint;

     previewWindow-&gt;setWindowFlags(flags);</pre>
<p>We also determine which of the hints that are checked, and add them to <tt>flags</tt> using an OR operator. We use <tt>flags</tt> to set the window flags for the preview window.</p>
<pre>     QPoint pos = previewWindow-&gt;pos();
     if (pos.x() &lt; 0)
         pos.setX(0);
     if (pos.y() &lt; 0)
         pos.setY(0);
     previewWindow-&gt;move(pos);
     previewWindow-&gt;show();
 }</pre>
<p>We adjust the position of the preview window. The reason we do that, is that playing around with the window's frame may on some platforms cause the window's position to be changed behind our back. If a window is located in the upper left corner of the screen, parts of the window may not be visible. So we adjust the widget's position to make sure that, if this happens, the window is moved within the screen's boundaries. Finally, we call <a href="qwidget.html#show">QWidget::show</a>() to make sure the preview window is visible.</p>
<pre> void ControllerWindow::createTypeGroupBox()
 {
     typeGroupBox = new QGroupBox(tr(&quot;Type&quot;));

     windowRadioButton = createRadioButton(tr(&quot;Window&quot;));
     dialogRadioButton = createRadioButton(tr(&quot;Dialog&quot;));
     sheetRadioButton = createRadioButton(tr(&quot;Sheet&quot;));
     drawerRadioButton = createRadioButton(tr(&quot;Drawer&quot;));
     popupRadioButton = createRadioButton(tr(&quot;Popup&quot;));
     toolRadioButton = createRadioButton(tr(&quot;Tool&quot;));
     toolTipRadioButton = createRadioButton(tr(&quot;Tooltip&quot;));
     splashScreenRadioButton = createRadioButton(tr(&quot;Splash screen&quot;));
     windowRadioButton-&gt;setChecked(true);

     QGridLayout *layout = new QGridLayout;
     layout-&gt;addWidget(windowRadioButton, 0, 0);
     layout-&gt;addWidget(dialogRadioButton, 1, 0);
     layout-&gt;addWidget(sheetRadioButton, 2, 0);
     layout-&gt;addWidget(drawerRadioButton, 3, 0);
     layout-&gt;addWidget(popupRadioButton, 0, 1);
     layout-&gt;addWidget(toolRadioButton, 1, 1);
     layout-&gt;addWidget(toolTipRadioButton, 2, 1);
     layout-&gt;addWidget(splashScreenRadioButton, 3, 1);
     typeGroupBox-&gt;setLayout(layout);
 }</pre>
<p>The private <tt>createTypeGroupBox()</tt> function is called from the constructor.</p>
<p>First we create a group box, and then we create a radio button (using the private <tt>createRadioButton()</tt> function) for each of the available types among the window flags. We make <a href="qt.html#WindowType-enum">Qt::Window</a> the initially applied type. We put the radio buttons into a <a href="qgridlayout.html">QGridLayout</a> and install the layout on the group box.</p>
<p>We do not include the default <a href="qt.html#WindowType-enum">Qt::Widget</a> type. The reason is that it behaves somewhat different than the other types. If the type is not specified for a widget, and it has no parent, the widget is a window. However, if it has a parent, it is a standard child widget. The other types are all top-level windows, and since the hints only affect top-level windows, we abandon the <a href="qt.html#WindowType-enum">Qt::Widget</a> type.</p>
<pre> void ControllerWindow::createHintsGroupBox()
 {
     hintsGroupBox = new QGroupBox(tr(&quot;Hints&quot;));

     msWindowsFixedSizeDialogCheckBox =
             createCheckBox(tr(&quot;MS Windows fixed size dialog&quot;));
     x11BypassWindowManagerCheckBox =
             createCheckBox(tr(&quot;X11 bypass window manager&quot;));
     framelessWindowCheckBox = createCheckBox(tr(&quot;Frameless window&quot;));
     windowTitleCheckBox = createCheckBox(tr(&quot;Window title&quot;));
     windowSystemMenuCheckBox = createCheckBox(tr(&quot;Window system menu&quot;));
     windowMinimizeButtonCheckBox = createCheckBox(tr(&quot;Window minimize button&quot;));
     windowMaximizeButtonCheckBox = createCheckBox(tr(&quot;Window maximize button&quot;));
     windowContextHelpButtonCheckBox =
             createCheckBox(tr(&quot;Window context help button&quot;));
     windowShadeButtonCheckBox = createCheckBox(tr(&quot;Window shade button&quot;));
     windowStaysOnTopCheckBox = createCheckBox(tr(&quot;Window stays on top&quot;));
     customizeWindowHintCheckBox= createCheckBox(tr(&quot;Customize window&quot;));

     QGridLayout *layout = new QGridLayout;
     layout-&gt;addWidget(msWindowsFixedSizeDialogCheckBox, 0, 0);
     layout-&gt;addWidget(x11BypassWindowManagerCheckBox, 1, 0);
     layout-&gt;addWidget(framelessWindowCheckBox, 2, 0);
     layout-&gt;addWidget(windowTitleCheckBox, 3, 0);
     layout-&gt;addWidget(windowSystemMenuCheckBox, 4, 0);
     layout-&gt;addWidget(windowMinimizeButtonCheckBox, 0, 1);
     layout-&gt;addWidget(windowMaximizeButtonCheckBox, 1, 1);
     layout-&gt;addWidget(windowContextHelpButtonCheckBox, 2, 1);
     layout-&gt;addWidget(windowShadeButtonCheckBox, 3, 1);
     layout-&gt;addWidget(windowStaysOnTopCheckBox, 4, 1);
     layout-&gt;addWidget(customizeWindowHintCheckBox, 5, 0);
     hintsGroupBox-&gt;setLayout(layout);
 }</pre>
<p>The private <tt>createHintsGroupBox()</tt> function is also called from the constructor.</p>
<p>Again, the first thing we do is to create a group box. Then we create a checkbox, using the private <tt>createCheckBox()</tt> function, for each of the available hints among the window flags. We put the checkboxes into a <a href="qgridlayout.html">QGridLayout</a> and install the layout on the group box.</p>
<pre> QCheckBox *ControllerWindow::createCheckBox(const QString &amp;text)
 {
     QCheckBox *checkBox = new QCheckBox(text);
     connect(checkBox, SIGNAL(clicked()), this, SLOT(updatePreview()));
     return checkBox;
 }</pre>
<p>The private <tt>createCheckBox()</tt> function is called from <tt>createHintsGroupBox()</tt>.</p>
<p>We simply create a <a href="qcheckbox.html">QCheckBox</a> with the provided text, connect it to the private <tt>updatePreview()</tt> slot, and return a pointer to the checkbox.</p>
<pre> QRadioButton *ControllerWindow::createRadioButton(const QString &amp;text)
 {
     QRadioButton *button = new QRadioButton(text);
     connect(button, SIGNAL(clicked()), this, SLOT(updatePreview()));
     return button;
 }</pre>
<p>In the private <tt>createRadioButton()</tt> function it is a <a href="qradiobutton.html">QRadioButton</a> we create with the provided text, and connect to the private <tt>updatePreview()</tt> slot. The function is called from <tt>createTypeGroupBox()</tt>, and returns a pointer to the button.</p>
<a name="previewwindow-class-definition"></a>
<h2>PreviewWindow Class Definition</h2>
<pre> class PreviewWindow : public QWidget
 {
     Q_OBJECT

 public:
     PreviewWindow(QWidget *parent = 0);

     void setWindowFlags(Qt::WindowFlags flags);

 private:
     QTextEdit *textEdit;
     QPushButton *closeButton;
 };</pre>
<p>The <tt>PreviewWindow</tt> class inherits <a href="qwidget.html">QWidget</a>. It is a custom widget that displays the names of its currently set window flags in a read-only text editor. It is also provided with a <a href="porting4.html#qpushbutton">QPushbutton</a> that closes the window.</p>
<p>We reimplement the constructor to create the <b>Close</b> button and the text editor, and the <a href="qwidget.html#windowFlags-prop">QWidget::setWindowFlags</a>() function to display the names of the window flags.</p>
<p align="center"><img src="images/windowflags_previewwindow.png" alt="Screenshot of the Preview Window" /></p><a name="previewwindow-class-implementation"></a>
<h2>PreviewWindow Class Implementation</h2>
<pre> PreviewWindow::PreviewWindow(QWidget *parent)
     : QWidget(parent)
 {
     textEdit = new QTextEdit;
     textEdit-&gt;setReadOnly(true);
     textEdit-&gt;setLineWrapMode(QTextEdit::NoWrap);

     closeButton = new QPushButton(tr(&quot;&amp;Close&quot;));
     connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));

     QVBoxLayout *layout = new QVBoxLayout;
     layout-&gt;addWidget(textEdit);
     layout-&gt;addWidget(closeButton);
     setLayout(layout);

     setWindowTitle(tr(&quot;Preview&quot;));
 }</pre>
<p>In the constructor, we first create a <a href="qtextedit.html">QTextEdit</a> and make sure that it is read-only.</p>
<p>We also prohibit any line wrapping in the text editor using the <a href="qtextedit.html#lineWrapMode-prop">QTextEdit::setLineWrapMode</a>() function. The result is that a horizontal scrollbar appears when a window flag's name exceeds the width of the editor. This is a reasonable solution since we construct the displayed text with built-in line breaks. If no line breaks were guaranteed, using another <a href="qtextedit.html#LineWrapMode-enum">QTextEdit::LineWrapMode</a> would perhaps make more sense.</p>
<p>Then we create the <b>Close</b> button, and put both the widgets into a <a href="qvboxlayout.html">QVBoxLayout</a> before we set the window title.</p>
<pre> void PreviewWindow::setWindowFlags(Qt::WindowFlags flags)
 {
     QWidget::setWindowFlags(flags);

     QString text;

     Qt::WindowFlags type = (flags &amp; Qt::WindowType_Mask);
     if (type == Qt::Window) {
         text = &quot;Qt::Window&quot;;
     } else if (type == Qt::Dialog) {
         text = &quot;Qt::Dialog&quot;;
     } else if (type == Qt::Sheet) {
         text = &quot;Qt::Sheet&quot;;
     } else if (type == Qt::Drawer) {
         text = &quot;Qt::Drawer&quot;;
     } else if (type == Qt::Popup) {
         text = &quot;Qt::Popup&quot;;
     } else if (type == Qt::Tool) {
         text = &quot;Qt::Tool&quot;;
     } else if (type == Qt::ToolTip) {
         text = &quot;Qt::ToolTip&quot;;
     } else if (type == Qt::SplashScreen) {
         text = &quot;Qt::SplashScreen&quot;;
     }

     if (flags &amp; Qt::MSWindowsFixedSizeDialogHint)
         text += &quot;\n| Qt::MSWindowsFixedSizeDialogHint&quot;;
     if (flags &amp; Qt::X11BypassWindowManagerHint)
         text += &quot;\n| Qt::X11BypassWindowManagerHint&quot;;
     if (flags &amp; Qt::FramelessWindowHint)
         text += &quot;\n| Qt::FramelessWindowHint&quot;;
     if (flags &amp; Qt::WindowTitleHint)
         text += &quot;\n| Qt::WindowTitleHint&quot;;
     if (flags &amp; Qt::WindowSystemMenuHint)
         text += &quot;\n| Qt::WindowSystemMenuHint&quot;;
     if (flags &amp; Qt::WindowMinimizeButtonHint)
         text += &quot;\n| Qt::WindowMinimizeButtonHint&quot;;
     if (flags &amp; Qt::WindowMaximizeButtonHint)
         text += &quot;\n| Qt::WindowMaximizeButtonHint&quot;;
     if (flags &amp; Qt::WindowContextHelpButtonHint)
         text += &quot;\n| Qt::WindowContextHelpButtonHint&quot;;
     if (flags &amp; Qt::WindowShadeButtonHint)
         text += &quot;\n| Qt::WindowShadeButtonHint&quot;;
     if (flags &amp; Qt::WindowStaysOnTopHint)
         text += &quot;\n| Qt::WindowStaysOnTopHint&quot;;
     if (flags &amp; Qt::CustomizeWindowHint)
         text += &quot;\n| Qt::CustomizeWindowHint&quot;;

     textEdit-&gt;setPlainText(text);
 }</pre>
<p>In our reimplementation of the <tt>setWindowFlags()</tt> function, we first set the widgets flags using the <a href="qwidget.html#windowFlags-prop">QWidget::setWindowFlags</a>() function. Then we run through the available window flags, creating a text that contains the names of the flags that matches the <tt>flags</tt> parameter. Finally, we display the text in the widgets text editor.</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
